<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
</head>
<body>
    <form action="/community/demo/registAjax" method="post">

        用户名:<input name="username" id="username" type="text" /><span  id="msg" style="color: green;"></span><br>

        密码:<input name="password" type="password"> <br>
        <input type="submit" value="注册">

    </form>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
    <script>
        $(function (){


            $("#username").on("blur",function(){
                $.post(
                    "/community/demo/registAjax",
                    {"username":$("#username").val()},
                    function(data){
                        data = $.parseJSON(data);
                        if(data.code){
                            //设置msg的文本值
                            $("#msg").text(data.msg)
                            //设置css属性
                            $("#msg").css("color","green")
                        }else {
                            $("#msg").text(data.msg)

                            $("#msg").css("color", "red")
                        }
                    }
                )
            })
        })
    </script>
</body>
</html>